<template>
  <el-row>
    <el-col :span="24">
      <el-card>
        <el-row>


          <!--username: '',-->
          <!--password: '',-->
          <!--project_id: '',-->
          <!--activity_id: '',-->
          <!--cookie: '',-->

          <el-col>
            <el-form :label-position="'left'" label-width="80px" :model="userMsg">
              <el-form-item label="项目ID">
                <el-input v-model="userMsg.project_id"></el-input>
              </el-form-item>
              <el-form-item label="活动ID">
                <el-input v-model="userMsg.activity_id"></el-input>
              </el-form-item>
              <el-form-item label="房ID">
                <el-input v-model="userMsg.vid"></el-input>
              </el-form-item>
              <el-form-item label="用户名(手机号码)">
                <el-input v-model="userMsg.username"></el-input>
              </el-form-item>
              <el-form-item label="密码">
                <el-input v-model="userMsg.password"></el-input>
              </el-form-item>
              <el-form-item label="COOKIE">
                <el-input disabled v-model="Array(userMsg.cookie).toString()"></el-input>
              </el-form-item>
              <el-form-item label="sale_user_login_token">
                <el-input disabled v-model="Array(userMsg.sale_user_login_token).toString()"></el-input>
              </el-form-item>

            </el-form>
          </el-col>

          <el-col>
            <el-form :label-position="'left'" label-width="80px" :model="encodeMsg">
              <el-form-item label="用户名加密">
                <el-input disabled v-model="encodeMsg.mobile"></el-input>
              </el-form-item>
              <el-form-item label="密码加密">
                <el-input disabled v-model="encodeMsg.password"></el-input>
              </el-form-item>
              <el-form-item label="密钥">
                <el-input disabled v-model="encodeMsg.key"></el-input>
              </el-form-item>
            </el-form>
          </el-col>


          <el-col :span="24">
            <el-button type="danger" @click="doLoginProcess">秒杀-执行登录流程</el-button>
          </el-col>

          <el-col :span="24">
            <el-button type="danger" @click="doKillProcess">秒杀-执行大量秒杀流程</el-button>
          </el-col>

          <el-col :span="24">
            <el-button type="success" @click="miaoshaLogin">秒杀-登录</el-button>
          </el-col>
          <el-col :span="24">
            <div>{{miaoshaLoginResponse}}</div>
          </el-col>

          <el-col :span="24">
            <el-button type="success" @click="miaoshaMyCollect">秒杀-我的收藏</el-button>
          </el-col>
          <el-col :span="24">
            <div>{{miaoshaMyCollectResponse}}</div>
          </el-col>

          <el-col :span="24">
            <el-button type="success" @click="miaoshaCheckUser">秒杀-检查登录</el-button>
          </el-col>
          <el-col :span="24">
            <div>{{miaoshaCheckUserResponse}}</div>
          </el-col>

          <el-col :span="24">
            <el-button type="success" @click="miaoshaEncode">秒杀-加密</el-button>
          </el-col>
          <el-col :span="24">
            <div>{{miaoshaEncodeResponse}}</div>
          </el-col>

          <el-col :span="24">
            <el-button type="success" @click="miaoshaSsoLogin">秒杀-单点登录</el-button>
          </el-col>
          <el-col :span="24">
            <div>{{miaoshaSsoLoginResponse}}</div>
            <div>{{userMsg.ssoCookie}}</div>
          </el-col>

          <el-col :span="24">
            <el-button type="success" @click="miaoshaKill">秒杀-杀杀杀</el-button>
          </el-col>
          <el-col :span="24">
            <div>{{miaoshaKillResponse}}</div>
          </el-col>

          <el-col :span="24">
            <el-button type="success" @click="miaoshaTrueKill">秒杀-杀杀杀-真！</el-button>
          </el-col>
          <el-col :span="24">
            <div>{{miaoshaTrueKillResponse}}</div>
          </el-col>

        </el-row>

      </el-card>

    </el-col>
  </el-row>
</template>

<script>
  import services from "@/store/services.js";

  export default {
    name: "MiaoShaCard",
    data() {
      return {
        resourceShow: false,
        userMsg: {
          username: 13913552139,
          password: 'x62658593w',
          project_id: 32856,// 32856
//          activity_id: 13776,// 13776 13774
          activity_id: 13775,// 13776 13774
          vid: 1261452,
          cookie: '',
          sale_user_login_token : '',
          ssoCookie: '',
        },
        encodeMsg: {
          mobile: '',
          password: '',
          key: '',
          check_image: '',
          _: '',
          callback: '',
        },
        miaoshaLoginResponse: '',
        miaoshaMyCollectResponse: '',
        miaoshaCheckUserResponse: '',
        miaoshaKillResponse: '',
        miaoshaTrueKillResponse: '',
        miaoshaEncodeResponse: '',
        miaoshaSsoLoginResponse: '',
      };
    },
    methods: {
//      showMyResource() {
//        this.resourceShow = true;
//      },
      miaoshaLogin() {
        services.miaoshaLogin(this.userMsg,{
          headers:{
            "content-type": "application/json"
          }
        }).then(result => {
          console.log('miaoshaLogin result data',result.data);
          this.miaoshaLoginResponse = result.data.responseBody;
          this.userMsg.cookie = result.data.cookie;
          this.userMsg.sale_user_login_token = result.data.sale_user_login_token;
        });
      },
      miaoshaCheckUser() {
        // 检查用户登录状态
        console.log('miaoshaCheckUser this.userMsg',this.userMsg);
        services.miaoshaCheckUser({
          userMsg: this.userMsg,
          "cookie": this.userMsg.sale_user_login_token,
//          "cookie": this.userMsg.cookie,

          // 用户名密码 工程id 活动id
        },{
          headers:{
            "Content-Type": "application/json"
          }
        }).then(result => {
          console.log('miaoshaCheckUser result',result);
          this.miaoshaCheckUserResponse = result.data.responseBody;
        });
      },
      miaoshaMyCollect() {
        console.log('miaoshaMyCollect this.userMsg',this.userMsg);

        services.miaoshaMyCollect({
          "cookie": this.userMsg.cookie,
          userMsg: this.userMsg
          // 用户名密码 工程id 活动id
        },{
          headers:{
            "Content-Type": "application/json"
          }
        }).then(result => {
          console.log('miaoshaMyCollect result',result);
          this.miaoshaMyCollectResponse = result.data.responseBody;
        });
      },
      miaoshaEncode(){
        this.promiseMiaoshaEncode();
//        // 秒杀
//        services.miaoshaEncode({
//          // cookie: this.userMsg.cookie,
//          "cookie": this.userMsg.sale_user_login_token,
//          userMsg: this.userMsg
//          // 用户名密码 工程id 活动id
//        },{
//          headers:{
//            "content-type": "application/json"
//          }
//        }).then(result => {
//          console.log('miaoshaEncode result',result);
//          this.encodeMsg.mobile = result.data.responseBody.mobile;
//          this.encodeMsg.password = result.data.responseBody.password;
//          this.encodeMsg.key = result.data.responseBody.key;
//
//          this.miaoshaEncodeResponse = result.data.responseBody;
//        });
      },
      promiseMiaoshaEncode(){
        return new Promise((resolve,reject)=>{
          // 秒杀
          services.miaoshaEncode({
            // cookie: this.userMsg.cookie,
            "cookie": this.userMsg.sale_user_login_token,
            userMsg: this.userMsg
            // 用户名密码 工程id 活动id
          },{
            headers:{
              "content-type": "application/json"
            }
          }).then(result => {
            console.log('miaoshaEncode result',result);
            this.encodeMsg.mobile = result.data.responseBody.mobile;
            this.encodeMsg.password = result.data.responseBody.password;
            this.encodeMsg.key = result.data.responseBody.key;
            this.miaoshaEncodeResponse = result.data.responseBody;
            resolve(result);
          }).catch(error => {
            resolve(error);
          });
        });
      },
      miaoshaSsoLogin(){
        this.promiseMiaoshaSsoLogin();
      },
      promiseMiaoshaSsoLogin(){
        return new Promise((resolve,reject) => {
          // 单点登录 获取用户cookie
          services.miaoshaSsoLogin({
            // "cookie": this.userMsg.cookie,
            userMsg: this.userMsg,
            // "cookie": this.userMsg.sale_user_login_token,
            encodeMsg: this.encodeMsg,
            // 用户名密码 工程id 活动id
          },{
            headers:{
              "content-type": "application/json"
            }
          }).then(result => {
            console.log('miaoshaSsoLogin result',result);
            this.userMsg.ssoCookie = result.data.ssoCookie;
            this.miaoshaSsoLoginResponse = result.data.responseBody;
            resolve(result);
          }).catch(error => {
            reject(error);
          });
        })
      },
      miaoshaKill(){
        this.promiseMiaoshaKill();
//        // 秒杀
//        services.miaoshaKill({
//          "cookie": this.userMsg.sale_user_login_token,
//
//          // cookie: this.userMsg.cookie,
//          // 用户名密码 工程id 活动id
//        },{
//          headers:{
//            "content-type": "application/json"
//          }
//        }).then(result => {
//          console.log('miaoshaKill result',result);
//          this.miaoshaKillResponse = result.data.responseBody;
//        });
      },
      miaoshaTrueKill(){
        this.promiseMiaoshaTrueKill();
      },
      promiseMiaoshaKill(){
        return new Promise((resolve,reject)=>{
          // 秒杀
          services.miaoshaKill({
            "cookie": this.userMsg.sale_user_login_token,
            userMsg: this.userMsg,

            // cookie: this.userMsg.cookie,
            // 用户名密码 工程id 活动id
          },{
            headers:{
              "content-type": "application/json"
            }
          }).then(result => {
            console.log('miaoshaKill result',result);
            this.miaoshaKillResponse = result.data.responseBody;
            resolve(result);
          }).catch(error => {
            reject(error);
          });
        });
      },

      promiseMiaoshaTrueKill(){
        return new Promise((resolve,reject)=>{
          // 秒杀
          services.miaoshaTrueKill({
            "cookie": this.userMsg.sale_user_login_token,
            userMsg: this.userMsg,

            // cookie: this.userMsg.cookie,
            // 用户名密码 工程id 活动id
          },{
            headers:{
              "content-type": "application/json"
            }
          }).then(result => {
            console.log('miaoshaTrueKill result',result);
            this.miaoshaTrueKillResponse = result.data.responseBody;
            resolve(result);
          }).catch(error => {
            reject(error);
          });
        });
      },
      doLoginProcess(){
        // encode
        this.promiseMiaoshaEncode().then((res)=>{
          // ssoLogin
          return this.promiseMiaoshaSsoLogin()
        }).then((res)=>{
          // 流程完成
        })
      },
      doKillProcess(){
        // many times kill
        // 100毫秒发一组,连续发送1秒(1000毫秒，10次) ====> 拓展为一分钟
        // 一组10个
        let pAllProcess = 0;
        let pAllTime = 600; // 10 ===> 60*10 (600)
        let pAllContentCount = 10;
        let delay = 100;
        let mainPromise = this.promiseMiaoshaKill();
        let mainPromiseTrue = this.promiseMiaoshaTrueKill();
        let mainPromiseArr = [];
        let mainPromiseTrueArr = [];

        for (let i = 0; i < pAllContentCount; i++) {
          mainPromiseArr.push(mainPromise);
          mainPromiseTrueArr.push(mainPromiseTrue)
        }

        let timer = setInterval(()=>{
          if(pAllProcess < pAllTime){
            pAllProcess++;

//            Promise.all(mainPromiseArr).then((res)=>{
//              console.log(`doKillProcess ${pAllProcess} succ`,res)
//            }).catch((rej)=>{
//              console.log(`doKillProcess ${pAllProcess} err`,rej)
//            }).finally(()=>{
//
//            })

            Promise.all(mainPromiseTrueArr).then((res)=>{
              console.log(`doTrueKillProcess ${pAllProcess} succ`,res)
            }).catch((rej)=>{
              console.log(`doTrueKillProcess ${pAllProcess} err`,rej)
            }).finally(()=>{

            })

          }else{
            clearInterval(timer);
          }
        },delay);
      }


    },
    computed: {
//      ...mapGetters(["basicInfo", "loginState"]),
//      newSourceData() {
//        return renderTreeData({ docs: this.basicInfo.resources });
//      }
    },
    mounted() {
//      this.$store.dispatch("getSiteBasicInfo");
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
